package com.wang.vedio.utils;

import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

import android.annotation.SuppressLint;
import android.text.TextUtils;

/***
 * 时间管理
 */
@SuppressLint("SimpleDateFormat")
public class TimeUtils {

	/**
	 * 将字符串类型的时间转换成long类型的  (格式化类型,比如2014-12-16 09:49:00)
	 */
	public static long getTime(String date, String formatType) {
		SimpleDateFormat format = new SimpleDateFormat(formatType);
		ParsePosition position = new ParsePosition(0);
		Date t = format.parse(date, position);
		long time = t.getTime();
		return time;
	}

	/**
	 * 将字符串类型的时间转换成long类型的  (格式化类型,比如2014-12-16 09:49:00)
	 */
	public static String getNowYear() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
		Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
		String str = formatter.format(curDate);
		return str;
	}

	/**
	 * 将long类型的时间转换成字符串类型的(格式化类型, 比如2014-12-16 09:49:00)
	 */
	public static String getTime(long date, String formatType) {
		if(date == 0l){
			return "无";
		}
		SimpleDateFormat format = new SimpleDateFormat(formatType);
		String time = format.format(date);
		return time;
	}

	/***
	 * 获得当前日期(2014-04-02 字符串)
	 */
	public static String getCurrentTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
		String str = formatter.format(curDate);
		return str;
	}


	/***
	 * 获得当前日期(2014-04-02 字符串)
	 */
	public static String getCurrentTime(String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
		String str = formatter.format(curDate);
		return str;
	}

	public static String dataToStr(Date data,String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		String str = formatter.format(data);
		return str;
	}

	/***
	 * 获得当前时间(20140402121822 字符串)
	 */
	public static String getNowTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
		String str = formatter.format(curDate);
		return str;
	}

	/***
	 * 获得当前日期(2014-04-02 Date类型)
	 */
	public static Date getCurrentDate() {
		Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
		return curDate;
	}

	/**
	 * 根据一个日期，返回是星期几的字符串
	 */
	public static String getWeek(String sdate) {
		// 再转换为时间
		Date date = strToDate(sdate);
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int weeks = c.get(Calendar.WEEK_OF_MONTH);
		c.setFirstDayOfWeek(weeks);
		weeks = c.get(Calendar.WEEK_OF_MONTH);
		System.out.println("---week当前周===" + weeks);
		return new SimpleDateFormat("EEEE").format(c.getTime());
	}

	/**
	 * 将短时间格式字符串转换为时间 yyyy-MM-dd
	 */
	public static Date strToDate(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}

	public static Calendar strToCalendar(String strDate) {
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			Date date = formatter.parse(strDate);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			return calendar;
		} catch (Exception e) {
			return null;
		}
	}




	/**
	 * 得到日期对应的weekDay
	 */
	public static String getWeekStr(String sdate) {
		String str = "";
		str = getWeek(sdate);
		if ("1".equals(str)) {
			str = "星期日";
		} else if ("2".equals(str)) {
			str = "星期一";
		} else if ("3".equals(str)) {
			str = "星期二";
		} else if ("4".equals(str)) {
			str = "星期三";
		} else if ("5".equals(str)) {
			str = "星期四";
		} else if ("6".equals(str)) {
			str = "星期五";
		} else if ("7".equals(str)) {
			str = "星期六";
		}
		System.out.println("--当前星期几---+++" + str);
		return str;
	}

	/** 产生周序列,即得到当前时间所在的年度是第几周 */
	public static String getSeqWeek() {
		Calendar c = Calendar.getInstance(Locale.CHINA);
		String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
		if (week.length() == 1)
			week = "0" + week;
		String year = Integer.toString(c.get(Calendar.YEAR));
		return year + week;
	}

	/**
	 * 去除时分秒
	 */
	public static String parseStr(String str) {
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			return dateFormat.format(dateFormat.parse(str));
		} catch (Exception e) {
		}
		return null;
	}

	public static String SaleParseStr(String str) {
		long time = getTime(str, "yyyy-MM-dd");
		String returnStr = "";
		// 传参进来的时间 Calendar
		Calendar oldC = Calendar.getInstance();
		oldC.setTimeInMillis(time);
		// 当前系统时间 Calendar
		Calendar nowC = Calendar.getInstance();
		nowC.setTimeInMillis(System.currentTimeMillis());
		// 传参时间的 年-日
		int oldY = oldC.get(Calendar.YEAR);
		// 当前系统时间 年-日
		int nowY = nowC.get(Calendar.YEAR);
		if (oldY == nowY) {
			returnStr = getTime(time, "MM-dd");
		} else {
			returnStr = getTime(time, "yyyy-MM-dd"); // yyyy-MM-dd
		}
		return returnStr;
	}

	/**
	 * 去除年时分秒
	 */
	public static String parseStrYearMD1(String str) {
		SimpleDateFormat format = new SimpleDateFormat("MM-dd");
		long time = getTime(str);
		return format.format(time);
	}

	/**
	 * 时间字符串去除秒
	 */
	public static String timeParseStr(String timeStr) {
		long time = getTime(timeStr);
		String returnStr = "";
		// 传参进来的时间 Calendar
		Calendar oldC = Calendar.getInstance();
		oldC.setTimeInMillis(time);
		// 当前系统时间 Calendar
		Calendar nowC = Calendar.getInstance();
		nowC.setTimeInMillis(System.currentTimeMillis());
		// 传参时间的 年-日
		int oldY = oldC.get(Calendar.YEAR);
		// 当前系统时间 年-日
		int nowY = nowC.get(Calendar.YEAR);
		if (oldY == nowY) {
			returnStr = getTime(time, "MM-dd HH:mm");
		} else {
			returnStr = getTime(time, "yy-MM-dd HH:mm");
		}
		return returnStr;
	}

	/**
	 * 时间字符串去除秒
	 */
	public static String timeParseStr(String timeStr, String formatType) {
		long time = getTime(timeStr, formatType);
		return getTime(time, formatType);
	}

	public static String parseTimeStr(String str) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			SimpleDateFormat sdf1 = new SimpleDateFormat("MM-dd HH:mm:ss");
			return sdf1.format(sdf.parse(str));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 字符串时间比较(传值是否大于当前时间)
	 */
	public static boolean timeCompare(String time) {
		long t = System.currentTimeMillis();
		long t1 = getTime(time);
		if (t > t1) {
			return true;
		} else {
			return false;
		}
	}

	// 将字符串类型的时间转换成long类型的
	public static long getTime(String date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ParsePosition position = new ParsePosition(0);
		Date t = format.parse(date, position);
		long time = t.getTime();
		return time;
	}

	/**
	 * 时间格式化, 去掉年份 显示07-03 HH:mm:ss
	 */
	public static String timeFormat(String time) {
		if (TextUtils.isEmpty(time)) {
			return "";
		}
		long t = getTime(time);
		SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
		return format.format(t);
	}

	/**
	 * 时间格式化, 去掉年月 显示HH:mm
	 * 
	 * @param time
	 * @return
	 */
	public static String timeFormatHHmm(String time) {
		if (TextUtils.isEmpty(time)) {
			return "";
		}
		long t = getTime(time);
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		return format.format(t);
	}

	/**
	 * 字符串时间比较(参数1 跟参数2比较)
	 */
	public static boolean timeCompare(String time1, String time2) {
		long t1 = getTime(time1);
		long t2 = getTime(time2);
		if (t1 > t2) {
			return true;
		}
		return false;

	}

	/**
	 * 时间差，转换成分钟
	 */
	public static int timecha(String startTime, String endTime) {
		int cha = 0;
		long between = (getTime(endTime) - getTime(startTime)) / 1000;
		long day1 = between / (24 * 3600);
		long hour1 = between % (24 * 3600) / 3600;
		long minute1 = between % 3600 / 60;
		cha = (int) (day1 * 24 * 60 + hour1 * 60 + minute1);
		return cha;
	}

	/**
	 * long 类型时间解析MM-dd HH:mm:ss
	 */
	public static String getTimeFormt(long date) {
		SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm:ss");
		String time = format.format(date);
		return time;
	}

	/**
	 * 获取当前季度
	 */
	public static String getQuarter() {
		String quarter = "";
		int month = -1;
		int year = -1;
		GregorianCalendar d = new GregorianCalendar();
		int weekday = d.get(Calendar.WEEK_OF_MONTH);
		System.out.println(weekday);
		final Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		year = c.get(Calendar.YEAR);
		month = c.get(Calendar.MONTH) + 1;
		if (month == 1 || month == 2 || month == 3) {
			quarter = "1季度";
			System.out.println("1季度");
		} else if (month == 4 || month == 5 || month == 6) {
			quarter = "2季度";
			System.out.println("2季度");
		} else if (month == 7 || month == 8 || month == 9) {
			quarter = "3季度";
			System.out.println("3季度");
		} else if (month == 10 || month == 11 || month == 12) {
			quarter = "4季度";
			System.out.println("4季度");
		}
		System.out.println(quarter + "year====" + year);
		return year + "-" + quarter;
	}

	/**
	 * 获取年月, 返回int[]
	 */
	public static int[] getYearMonth() {
		final Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		int[] ym = new int[2];
		ym[0] = year;
		ym[1] = month;
		return ym;
	}

	/***
	 * 比较两个日期之间的大小2014-04-24
	 */
	public static int compare_date(String DATE1, String DATE2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			if (null != DATE1 && null != DATE2) {
				Date dt1 = df.parse(DATE1);
				Date dt2 = df.parse(DATE2);
				if (dt1.getTime() > dt2.getTime()) {
					return 1;
				} else if (dt1.getTime() < dt2.getTime()) {
					return -1;
				} else {
					return 0;// 当前日期
				}
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	/**
	 * 根据传入的参数计算出距离当前时间几秒前, 几分钟前
	 * @param gap 时间字符串 2014-12-16 08:30:00
	 */
	public static String computeFromNowStr(String gap) {
		return computeFromNowStr(getTime(gap));
	}

	/**
	 * 根据传入的参数计算出距离当前时间几秒前, 几分钟前
	 * 
	 * @param gap
	 *            long类型毫秒
	 * @return
	 */
	public static String computeFromNowStr(long gap) {
		long time = gap;
		if (gap < 0) {
			gap = 0;
		}
		long a = (System.currentTimeMillis() / 1000) - (gap / 1000);
		// 防止本地时间大于服务器时间
		if (a < 0) {
			return dataFormatComputeNow(time); // 时间工具类 返回的是字符串类型的时间
		}
		gap = Math.abs(a);
		final String[] danweis = new String[] { "秒前", "分钟前", "小时前", "天前" };
		String danwei = danweis[0];
		if (gap >= 60) {
			gap = gap / 60;// 以分钟为单位
			danwei = danweis[1];
			if (gap >= 60) {
				gap = gap / 60;// 以小时为单位
				danwei = danweis[2];
				if(gap > 24){
					gap = gap / 24;// 以天为单位
					danwei = danweis[3];
					if(gap > 7){
						return dataFormatComputeNow(time); // 时间工具类 返回的是字符串类型的时间
					}
				}
			}
		}
		return gap + danwei;
	}

	/**
	 * 时间格式化
	 * 
	 * @param time
	 *            String 字符串类型
	 * @param formatType
	 *            时间类型的格式化 格式
	 * @return
	 */
	public static String dataFormatComputeNow(String time, String formatType) {
		return dataFormatComputeNow(getTime(time, formatType));
	}

	/**
	 * 时间格式化
	 * 
	 * @param time long类型毫秒
	 * @return 今天 时间 , 昨天, 星期几, 日期
	 */
	public static String dataFormatComputeNow(long time) {
		String returnStr = "";
		// 传参进来的时间 Calendar
		Calendar oldC = Calendar.getInstance();
		oldC.setTimeInMillis(time);
		// 当前系统时间 Calendar
		Calendar nowC = Calendar.getInstance();
		nowC.setTimeInMillis(System.currentTimeMillis());

		// 传参时间的 年-日
		int oldY = oldC.get(Calendar.YEAR);
		int oldM = oldC.get(Calendar.MONTH);
		int oldD = oldC.get(Calendar.DAY_OF_MONTH);
		// 当前星期几, 得到的值必须-1
		int oldWeek = oldC.get(Calendar.DAY_OF_WEEK) - 1;

		// 当前系统时间 年-日
		int nowY = nowC.get(Calendar.YEAR);
		int nowM = nowC.get(Calendar.MONTH);
		int nowD = nowC.get(Calendar.DAY_OF_MONTH);
		if (oldY == nowY) {
			if (nowM - oldM == 0 && nowD - oldD > -1) {
				// 说明在一个星期内 显示昨天或者星期几
				if (nowD - oldD == 0) {
					// 当天
					returnStr = "今天 " + getTime(time, "HH:mm");
				} else {
					returnStr = getTime(time, "MM-dd");
				}
			} else {
				// 同年 显示日期即可
				returnStr = getTime(time, "MM-dd");
			}

		} else {
			returnStr = getTime(time, "yyyy-MM-dd");
		}

		return returnStr;
	}

	/**
	 * 聊天详情调用
	 * 
	 * @param time
	 * @return
	 */
	public static String dataFormatChat(long time) {
		String returnStr = "";
		// 传参进来的时间 Calendar
		Calendar oldC = Calendar.getInstance();
		oldC.setTimeInMillis(time);
		// 当前系统时间 Calendar
		Calendar nowC = Calendar.getInstance();
		nowC.setTimeInMillis(System.currentTimeMillis());

		// 传参时间的 年-日
		int oldY = oldC.get(Calendar.YEAR);
		int oldD = oldC.get(Calendar.DAY_OF_MONTH);

		// 当前系统时间 年-日
		int nowY = nowC.get(Calendar.YEAR);
		int nowD = nowC.get(Calendar.DAY_OF_MONTH);

		if (oldY == nowY) {
			// 说明在当年, 同年 显示日期即可
			if (nowD - oldD == 0) {
				// 当天
				returnStr = getTime(time, "HH:mm:ss");
			} else {
				returnStr = getTime(time, "MM-dd HH:mm:ss");
			}

		} else {
			// 判断不在当年显示全部日期
			returnStr = getTime(time, "yyyy-MM-dd HH:mm:ss");
		}
		return returnStr;
	}
}

